/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.webservice.client;

import com.edu.kamai.client.control.paperControl;
import com.edu.kamai.webservice.paper.Paper;
import com.edu.kamai.webservice.paper.PaperService;
import com.edu.kamai.webservice.paper.PaperService_Service;


import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.soap.SOAPFaultException;

/**
 *
 * @author xyzhu
 */
public class WSPaperClient {
    private final QName qName = new QName("http://webservice.kamai.edu.com/",
            "paperService");
    private URL[] url;
    private int size;
    
    public WSPaperClient() throws Exception{
        size = paperControl.getNetList().size();
        url = new URL[size];
        for(int i=0;i<size;i++){
            url[i] = new URL("http://" + paperControl.getNetList().get(i) + 
                    "/paperService/paperService?WSDL");
        }
    }
    
    public int addOnePaper(String paperPath,String paperKey,String paperTitle,
            String paperDes,int paperRole) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int res = 0;
        do{
            try{
                System.out.println("addOnePaper : " + url[k]);
                PaperService_Service service = new PaperService_Service(url[k], 
                        qName);
                PaperService port = service.getPaperServicePort();
                res = port.addOnePaper(paperPath, paperKey, paperTitle, paperDes
                        , paperRole);
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return res;
    }
    
    public List<Paper> findPaperByRoleID(int roleID) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        List<Paper> retPaper = null;
        do{
            try{
                System.out.println("findPaperByRoleID : " + url[k]);
                PaperService_Service service = new PaperService_Service(url[k], 
                        qName);
                PaperService port = service.getPaperServicePort();
                retPaper = port.findPaperByRoleID(roleID);
                find = true;
                break;
            }catch(Exception e){
                e.printStackTrace();
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return retPaper;
    }
    
    public List<Paper> findPapers() throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        List<Paper> retPaper = null;
        do{
            try{
                System.out.println("findPaperByRoleID : " + url[k]);
                PaperService_Service service = new PaperService_Service(url[k], 
                        qName);
                PaperService port = service.getPaperServicePort();
                retPaper = port.findPapers();
                find = true;
                break;
            }catch(Exception e){
                e.printStackTrace();
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return retPaper;
    }
    
    public Paper findPaperByIDAndRole(int roleID,int pid) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        Paper retPaper = null;
        do{
            try{
                System.out.println("findPaperByIDAndRole : " + url[k]);
                PaperService_Service service = new PaperService_Service(url[k], 
                        qName);
                PaperService port = service.getPaperServicePort();
                retPaper = port.findPaperByIDAndRole(roleID,pid);
                find = true;
                break;
            }catch(Exception e){
                e.printStackTrace();
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return retPaper;
    }
    
    
}
